#!/usr/bin/env node

const Koa = require('koa')
const app = new Koa()

app.use(async(ctx, next) => { //M-logger
  //解析请求
  //next()
  //组装响应

  console.log(`${ctx.method} ${ctx.path}`)
  //next()
  //const cost = ctx.response.header.cost
  //console.log(`cost ${cost}ms`)
  
  //const cost = await next()
  //console.log(`cost ${cost}ms`)
  
  await next()
  console.log(`cost ${ctx.cost}ms`)  
})

app.use((ctx, next) => { //M-ms
  const start = Date.now()
  next()
  const end = Date.now()
  //console.log(`cost: ${end-start} ms`)
  
  //ctx.set('cost', end - start)//在响应头里传数据

  //return end-start
 
  ctx.cost = end - start
})

app.use((ctx, next) => { //M-hello
  //没有解析请求
  //没有调用next()
  ctx.body = 'hello world'//响应体
})

app.listen(8080)

console.log('hello koa2')
